﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2020/9/25 10:56:55 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;

namespace MTQ.Web.Controllers
{

    public class Base_RoleGroupController : BaseController
    {

        private IBase_RoleGroupService ibase_rolegroupService = new Base_RoleGroupService();


        //                                           
        // GET: //         
        [AccountAuth(Roles = "Base_RoleGroup_Index")]
        public ActionResult Index()
        {
            return View();
        }


        [AccountAuth(Roles = "Base_RoleGroup_Index")]
        public ActionResult Search()
        {

            Base_RoleGroupQuery base_rolegroupQuery = new Base_RoleGroupQuery();

            ServertBootStrapTable servertBootStrapTable = new ServertBootStrapTable();

            servertBootStrapTable = ibase_rolegroupService.LoadBootstrapSearchData(base_rolegroupQuery, Request);

            return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SearchAll()
        {

            Base_RoleGroupService groupService = new Base_RoleGroupService();
            List<Base_RoleGroup> listGroup = groupService.LoadEntities(x => x.BG_Available == EnumAvailable.Enable).ToList();

            return Json(listGroup, JsonRequestBehavior.AllowGet);
        }


        //          
        // GET: /Base_RoleGroup/Create          
        [AccountAuth(Roles = "Base_RoleGroup_Create")]
        public ActionResult Create()
        {
            Base_RoleGroup base_rolegroup = new Base_RoleGroup();
            base_rolegroup.BG_Id = System.Guid.NewGuid();
            return View(base_rolegroup);
        }

        //          
        // POST: /Base_RoleGroup/Create          
        [HttpPost]
        [AccountAuth(Roles = "Base_RoleGroup_Create")]
        public ActionResult Create(Base_RoleGroup base_rolegroup)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            if (ModelState.IsValid)
            {
                var result = ibase_rolegroupService.AddEntity(base_rolegroup);
                listMsg.Add(new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败"));
            }
            return Json(listMsg);
        }


        [AccountAuth(Roles = "Base_RoleGroup_Edit")]
        public ActionResult Edit(Guid? primarykey)
        {
            Base_RoleGroup base_rolegroup = ibase_rolegroupService.LoadEntities(x => x.BG_Id == primarykey).FirstOrDefault();
            if (base_rolegroup == null)
            {
                return HttpNotFound();
            }
            return View(base_rolegroup);
        }

        [HttpPost]
        [AccountAuth(Roles = "Base_RoleGroup_Edit")]
        public ActionResult Edit(Base_RoleGroup formbase_rolegroup)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            if (ModelState.IsValid)
            {
                try
                {
                    Base_RoleGroup base_rolegroup = ibase_rolegroupService.LoadEntities(x => x.BG_Id == formbase_rolegroup.BG_Id).First();
                    base_rolegroup.BG_Id = formbase_rolegroup.BG_Id;
                    base_rolegroup.BG_Name = formbase_rolegroup.BG_Name;
                    base_rolegroup.BG_Remark = formbase_rolegroup.BG_Remark;
                    base_rolegroup.BG_Available = formbase_rolegroup.BG_Available;
                    bool result = ibase_rolegroupService.UpdateEntity(base_rolegroup);
                    listMsg.Add(new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败"));
                }
                catch (Exception e)
                {
                    listMsg.Add(new ModelMessage("ChangeResult", "m002", "保存失败"));
                }
            }
            return Json(listMsg);

        }

        [HttpPost]
        [AccountAuth(Roles = "Base_RoleGroup_Edit")]
        public ActionResult ActivateSwich(string primarykeys, string activate = "N")
        {
            ModelMessage modelMessage = new ModelMessage();

            if (OperatorAvailable(primarykeys, activate))
            {
                modelMessage.Code = "m001";
                modelMessage.Message = "修改成功";
            }
            else
            {
                modelMessage.Code = "m002";
            }
            return Json(modelMessage, JsonRequestBehavior.AllowGet);
        }

        private bool OperatorAvailable(string primarykeys, string available)
        {
            List<Guid> listBase_RoleGroupGUID = primarykeys.Split(',').ToListGuid();
            List<Base_RoleGroup> listBase_RoleGroup = ibase_rolegroupService.LoadEntities(x => listBase_RoleGroupGUID.Contains(x.BG_Id)).ToList();
            foreach (var item in listBase_RoleGroup)
            {
                item.BG_Available = available;
            }
            int updaterow = ibase_rolegroupService.UpdateEntity();
            if (updaterow == 0)
            {
                return false;
            }
            return true;
        }




    }
}


